$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var data_weight = 0;
    var pass = 1;

    //获取当前日期
    var val_today = new Date().Format("yyyy-MM-dd");
    //打包日期
    $("#dbrq").val(val_today);
    $("#dbrq").calendar({
        //value: ['2015-12-05']
    });

    //批号   获取 焦点
    $("input.cpph").focus();


    //吊秤 扫描 获取 重量
    $(".getWeight").on("click", function (event) {
        var val = $("input.cpph").val();
        if (!val) {
            $alert("请先扫描产品！");
        } else {
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/productinorder/getWeight',
                data: {
                    batch: $("input.cpph").val()
                },
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    if (res.success) {
                        //$alert("重量获取成功", 800);
                        $("input.cpmz").val(res.result.weight);
                        setCpjz();
                    } else {
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    loading_hide();
                    console.log(error);
                    $alert(error.responseText);
                }
            });
        }
    });

    //如果是扫描过来的
    if (localStorage.getItem("oneProductData")) {
        var data = JSON.parse(localStorage.getItem("oneProductData"));
        data_weight = data.availableQuantity;
        $("input.cpph").val(data.batch);
        $("input.cpmz").val("");
        $("input.cppz").val(data.tareQuantity || 0);
        $("input.cpjz").val("");
        $("input.cppcd").val(data.workplanNumber || "");
        $("input.cpkh").val(data.customerName || "");
        $("input.cpkw").val(data.location ? data.location : "");
        $("input.cplx").val(data.saleordertype ? data.saleordertype : "");
        $("input.cpgs").val(data.companyName ? data.companyName : "");
        $("input.alias").val(data.alias);
        $("input.cpqd").val(data.tensileStrength ? data.tensileStrength : "");
        $("input.cpxc").val(data.zincLayerWeight ? data.zincLayerWeight : "");
        $("input.cplh").val(data.stove ? data.stove : "");
        $("input.cprq").val(data.inTeamDate);
        $("input.ggxh").val(data.inventorySpecification);
        $("input.cpbm").val(data.inventoryCode);
        $("input.cpmc").val(data.inventoryName);
        $alert("信息填入成功！");
        localStorage.removeItem("oneProductData");
        $(".content .cpsczjjg").val("合格").trigger("change");
    }

    //点击返回箭头 清除 缓存 打开 home.html
    $(".head").on("click", "a.a_back", function () {
        localStorage.removeItem("pack_con");
        localStorage.removeItem("model");
        localStorage.removeItem("pack_data");
        localStorage.removeItem("productInWh");
        localStorage.removeItem("productInDp");
        window.location.href = "storekeeper.html";
    });

    //搜索 批号
    $("input.cpph").on("keyup", function (event) {
        if (event.keyCode == 13) {
            if (pass == 0) {
                return
            }
            pass = 0;
            if (!localStorage.getItem("model")) {
                $("input.ggxh").val("");
                $("input.cpbm").val("");
                $("input.cpmc").val("");
            }
            var val = $(this).val();
            if (!val) {
                pass = 1;
                $alert("批号不可为空！");
                return
            }
            var pack_con = JSON.parse(localStorage.getItem("pack_con"));
            if (pack_con && JSON.stringify(pack_con).indexOf(val) >= 0) {
                $("input.cpph").val("");
                pass = 1;
                return $alert("不可重复扫描！");
            }
            var firstBatch = "";
            if (pack_con && pack_con[0]) {
                firstBatch = pack_con[0].cpph;
            }
            // if (pack_con && pack_con.length > 20) {
            //     $("input.cpph").val("");
            //     pass = 1;
            //     return $alert("已达到20件了，请即时提交！");
            // }
            //获取 产品信息  在wd 中
            loading_show("搜索中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "POST",
                url: url + "/app/productinorder/getWareouseForProduct",
                data: {
                    batch: val,
                    firstBatch: firstBatch,
                },
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        var data = res.result;
                        //console.log(result);
                        if (pack_con && JSON.stringify(pack_con).indexOf(val) >= 0) {
                            $("input.cpph").val("");
                            return $alert("不可重复扫描！");
                        }
                        if (!data) {
                            $alert("此产品不存在！");
                            $("input.cpph").val("").focus();
                            return;
                        }
                        data_weight = data.availableQuantity;
                        $("input.cpmz").val("");
                        $("input.cppz").val(data.tareQuantity || 0);
                        $("input.cpjz").val("");
                        $("input.cppcd").val(data.workplanNumber || "");
                        $("input.cpkh").val(data.customerName || "");
                        $("input.cpkw").val(data.location ? data.location : "");
                        $("input.cplx").val(data.saleordertype ? data.saleordertype : "");
                        $("input.cpgs").val(data.inCompanyName ? data.inCompanyName : "");
                        $("input.alias").val(data.alias);
                        $("input.cprq").val(data.inTeamDate);
                        $("input.cpqd").val(data.tensileStrength ? data.tensileStrength : "");
                        $("input.cpxc").val(data.zincLayerWeight ? data.zincLayerWeight : "");
                        $("input.cplh").val(data.stove ? data.stove : "");
                        $("input.ggxh").val(data.inventorySpecification);
                        $("input.cpbm").val(data.inventoryCode);
                        $("input.cpmc").val(data.inventoryName);
                        $alert("信息填入成功！", 800);
                        $(".content .cpsczjjg").val("合格").trigger("change");
                    } else {
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    loading_hide();
                    console.log(error);
                    $alert(error.responseText);
                }
            });
        }
    });

    //库位扫描
    $(".content .cpkw").on("keyup", function (event) {
        if (event.keyCode == 13) {
            if (!$("input.cpkw").val()) {
                return $alert("库位不可为空！");
            } else {
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "post",
                    url: url + '/app/productinorder/getLocationForProduct',
                    data: {
                        batch: $("input.cpph").val(),
                        location: $("input.cpkw").val()
                    },
                    dataType: 'json',
                    json: 'callback',
                    cache: false,
                    success: function (res) {
                        if (res.success) {
                            var data = res.result;
                            $("input.cpkw").val(data.location ? data.location : "");
                        } else {
                            setTimeout(function () {
                                $("input.cpkw").val("").focus();
                            }, 1400);
                            $alert(res.message);
                        }
                    },
                    error: function (error) {
                        console.log(error);
                        setTimeout(function () {
                            $("input.cpkw").val("").focus();
                        }, 1400);
                        $alert(error.responseText);
                    }
                });
            }
        }
    });

    //皮重 毛重 失去 焦点 则 计算 净重
    $(".content").on("input", "input.cppz, input.cpmz", function () {
        setCpjz();
    });

    //实测直径 变化时，自动判断 是否合格
    $(".content").on("change", "input.cpsczj", function () {
        var cpsczj = $("input.cpsczj").val();
        if (cpsczj) {
            if (data.diameterToleranceMin && Number(cpsczj) < Number(data.diameterToleranceMin)) {
                $(".content .cpsczjjg").val("不合格").trigger("change");
            } else if (data.diameterToleranceMax && Number(cpsczj) > Number(data.diameterToleranceMax)) {
                $(".content .cpsczjjg").val("不合格").trigger("change");
            }
        }
    });


    //点击 add_receive 判断页面中的数据 打开pack_one.html
    $(".foot").on("click", ".add_receive", function () {
        setCpjz();
        //判断数据
        if (!$(".cpph").val()) {
            $alert("批号不可为空！");
            $(".cpph").focus();
            return;
        } else if (!$(".ggxh").val()) {
            $alert("存货规格不可为空！");
            return;
        } else if (!$(".cpmz").val()) {
            $alert("毛重不可为空！");
            return;
        } else if (!$(".cpkw").val()) {
            $alert("库位不可为空！");
            return;
        }
        //获取 打包信息
        var dbrq = $(".content .dbrq").val();
        var ggxh = $(".content .ggxh").val();
        var cpph = $(".content .cpph").val();
        var cpmz = $(".content .cpmz").val();
        var cppz = $(".content .cppz").val();
        var cpbm = $(".content .cpbm").val();
        var cpmc = $(".content .cpmc").val();
        var cpjz = $(".content .cpjz").val();
        var cppcd = $(".content .cppcd").val();
        var cpkh = $(".content .cpkh").val();
        var cpkw = $(".content .cpkw").val();
        var cplx = $(".content .cplx").val();
        var cpgs = $(".content .cpgs").val();
        var alias = $(".content .alias").val();
        var cprq = $("input.cprq").val();
        var cpqd =  $("input.cpqd").val();
        var cpxc =  $("input.cpxc").val();
        var cplh =  $("input.cplh").val();
        var cpsczj =  $("input.cpsczj").val();
        var cpsczjjg =  $(".content .cpsczjjg").val();
        if (Number(cpjz) <= 0) return $alert("净重不可小于0");
        if (!cpsczj) return $alert("实测直径不可为空");
        if (!cpsczjjg) return $alert("实测直径结果不可为空");
        var pack_con = [];
        if (localStorage.getItem("pack_con")) {
            pack_con = JSON.parse(localStorage.getItem("pack_con"));
        }
        if (cppz == "") {
            cppz = "0";
        }
        //判断库位
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/productinorder/getLocationForProduct',
            data: {
                batch: cpph,
                sczj: cpsczj,
                sczjjg: cpsczjjg,
                location: cpkw
            },
            dataType: 'json',
            json: 'callback',
            cache: false,
            success: function (res) {
                if (res.success) {
                    if (res.result.proInCheckResult == "不合格") {
                        $alert("实测直径不合格，不可入库!")
                    } else {
                        pack_con.unshift({
                            "dbrq": dbrq,
                            "ggxh": ggxh,
                            "cpph": cpph,
                            "cpmz": cpmz,
                            "cppz": cppz,
                            "cpbm": cpbm,
                            "cpmc": cpmc,
                            "cpjz": cpjz,
                            "cppcd": cppcd,
                            "cpkh": cpkh,
                            "cpkw": cpkw,
                            "cplx": cplx,
                            "cpgs": cpgs,
                            "cprq": cprq,
                            "cpqd": cpqd,
                            "cpxc": cpxc,
                            "cplh": cplh,
                            "cpsczj": cpsczj,
                            "cpsczjjg": cpsczjjg,
                            "alias": alias
                        });
                        localStorage.removeItem("oneProductData");
                        //缓存数据
                        localStorage.setItem("pack_con", JSON.stringify(pack_con));
                        //打开 pack_one.html
                        localStorage.removeItem("model");
                        localStorage.removeItem("pack_data");
                        window.location.href = "product_one.html";
                        //打印钢丝成品标签
                        res.virtualwarehouse.zincLayerWeight = cpxc;
                        res.virtualwarehouse.quantity = cpjz;
                        res.virtualwarehouse.availableQuantity = cpjz;
                        takeupPrint("wireProduct", res.virtualwarehouse);
                    }
                } else {
                    $alert(res.message);
                }
            },
            error: function (error) {
                console.log(error);
                setTimeout(function () {
                    $("input.cpkw").val("").focus();
                }, 1400);
                $alert(error.responseText);
            }
        });
    });

    //点击 del_receive  打开pack_one.html
    $(".foot").on("click", ".del_receive", function () {
        //打开 pack_one.html
        localStorage.removeItem("oneProductData");
        localStorage.removeItem("model");
        localStorage.removeItem("pack_data");
        window.location.replace("product_one.html");
    });


    //计算 净重
    function setCpjz() {
        var mz = $(".content input.cpmz").val();
        var pz = $(".content input.cppz").val();
        if (mz == "") {
            return;
        } else {
            if (pz == "") {
                pz = 0;
            }
            $(".content input.cpjz").val(Number(parseFloat(accSub(mz, pz)).toFixed(3)));
        }
    }

});